<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 引入vue2的js文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.9/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- <p>
        用户名：<input
          type="text"
          :value="username"
          @input="change($event,'username')"
        />
      </p>
      <p>
        密码：<input
          type="text"
          :value="password"
          @input="change($event,'password')"
        />
      </p> -->
      <p>
        用户名：<input
          type="text"
          name="username"
          :value="username"
          data-sex="男"
          @input="change"
        />
      </p>
      <p>
        密码：<input
          type="text"
          name="password"
          :value="password"
          data-sex="女"
          @input="change"
        />
      </p>
      <button @click="login">登录</button>
    </div>

    <script>
      // MVVVM中的ViewModel
      new Vue({
        el: '#app', // vue解析的节点
        data: {
          username: 'admin',
          password: '123'
        },
        methods: {
          login() {
            console.log(this.username, this.password)
          },
          /**
          changeUsername(e) {
            this.username = e.target.value
            
            this['username'] = e.target.value
          },
          changePassword(e) {
            this.password = e.target.value
          }
          **/
          change(e, val) {
            console.log(e.target.dataset)
            // this.username = e.target.value
            // this[val] = e.target.value

            // 属性名表达式
            this[e.target.name] = e.target.value
          }
        }
      })
    </script>
  </body>
</html>
